﻿using System;
using System.Linq.Expressions;

namespace NumberParser
{
    public static class Require
    {
        public static RequireArgument<T> Argument<T>(string name, T value)
        {
            return new RequireArgument<T>(name, value);
        }

        public static RequireArgument<T> Argument<T>(Expression<Func<T>> arg)
        {
            var exprBody = arg.Body as MemberExpression;
            if (exprBody == null)
            {
                throw new ArgumentException("Input expression must be a member accessor.", "arg");
            }

            var name = exprBody.Member.Name;
            var value = arg.Compile()();

            return new RequireArgument<T>(name, value);
        }

        public class RequireArgument<T>
        {
            #region Constructors

            internal RequireArgument(string name, T value)
            {
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentException("Cannot be null or empty.", "name");
                }

                Name = name;
                Value = value;
            }

            #endregion

            #region Properties

            /// <summary>
            /// Get the name of the argument being tested.
            /// </summary>
            public string Name { get; private set; }

            /// <summary>
            /// Get the value of the argument being tested.
            /// </summary>
            public T Value { get; private set; }

            #endregion
        }
    }
}